package com.no1.musicmgr.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.no1.musicmgr.entity.Music;
import com.no1.musicmgr.entity.Song;
import com.no1.musicmgr.entity.User;
import com.no1.musicmgr.mapper.MusicMapper;
import com.no1.musicmgr.service.MusicService;
import com.no1.musicmgr.service.SongService;
import com.no1.musicmgr.service.UserService;
import com.no1.musicmgr.util.DateUtil;
import com.no1.musicmgr.util.UUIDUtil;
import com.no1.musicmgr.vo.PageQueryBean;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Log4j
@RestController
@Tag(name = "MusicController", description = "音乐管理模块")
public class MusicController {
    @Autowired
    private UserService userService;

    @Autowired
    private MusicService musicService;

    @Autowired
    private SongService songService;

    @Operation(summary = "添加音乐")
    @PostMapping("/music")
    @Parameter(name = "music", description = "音乐对象")
    public boolean insertMusic(@RequestBody Music music) {
        System.out.println("传过来的数据：" + music);
        return musicService.insertMusic(music);
    }

    @Operation(summary = "删除音乐")
    @DeleteMapping("/music/{musicId}")
    @Parameter(name = "musicId", description = "音乐id")
    public boolean deleteMusicById(@PathVariable("musicId") Integer musicId) {
        return musicService.deleteMusicById(musicId);
    }

    @Operation(summary = "修改音乐")
    @PutMapping("/music")
    @Parameter(name = "music", description = "音乐对象，封装图书信息")
    protected boolean update(Music music) {
        log.info("传过来的数据：" + music);
        boolean rel = musicService.updateMusic(music);
        log.info("musicService：" + rel);
        return rel;
    }


    @Operation(summary = "查询音乐")
    @GetMapping("/music/list")
    @Parameter(name = "music", description = "音乐对象，封装查询条件")
    @Parameter(name = "page", description = "分页信息")
    private PageInfo<Music> selectList(Music music, PageQueryBean page) {
        // 分页设置
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        log.info("查询的music:" + music);
        String t1 = music.getMusic_time();
        if (t1 != null && !t1.isEmpty()) {
            if (t1.length() == 4) {
                if (Integer.parseInt(t1) < 1232) {
                    music.setMusic_time(DateUtil.formatDate(t1));
                }
            } else {
                music.setMusic_time(DateUtil.formatDate(t1));
            }
        }
        //时间格式转化
        String t2 = music.getMusic_date();
        if (t2 != null && !t2.isEmpty()) {
            if (t2.length() == 4) {
                if (Integer.parseInt(t2) < 1232) {
                    music.setMusic_date(DateUtil.formatDate(t2));
                }
            } else {
                music.setMusic_date(DateUtil.formatDate(t2));
            }
        }
        log.info("设置后的music_time:" + music.getMusic_time());
        log.info("设置后的music_date:" + music.getMusic_date());
        // 查询
        ArrayList<Music> list = musicService.listMusic(music);

        return new PageInfo<Music>(list);
    }

    @Operation(summary = "查询音乐")
    @GetMapping("/music/list1")
    @Parameter(name = "music", description = "音乐对象，封装查询条件")
    @Parameter(name = "page", description = "分页信息")
    private PageInfo<Music> selectList1(Music music, PageQueryBean page) {
        // 分页设置
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        log.info("查询的music:" + music);
        String t1 = music.getMusic_time();
        if (t1 != null && !t1.isEmpty()) {
            if (t1.length() == 4) {
                if (Integer.parseInt(t1) < 1232) {
                    music.setMusic_time(DateUtil.formatDate(t1));
                }
            } else {
                music.setMusic_time(DateUtil.formatDate(t1));
            }
        }
        //时间格式转化
        String t2 = music.getMusic_date();
        if (t2 != null && !t2.isEmpty()) {
            if (t2.length() == 4) {
                if (Integer.parseInt(t2) < 1232) {
                    music.setMusic_date(DateUtil.formatDate(t2));
                }
            } else {
                music.setMusic_date(DateUtil.formatDate(t2));
            }
        }
        log.info("设置后的music_time:" + music.getMusic_time());
        log.info("设置后的music_date:" + music.getMusic_date());
        // 查询
        ArrayList<Music> list = musicService.list1Music(music);
        for(int i=0;i<list.size();i++){
            list.get(i).setUser_img((userService.select1(list.get(i).getMusic_user_name())).getUser_tx());
        }
        return new PageInfo<Music>(list);
    }


    @Operation(summary = "我的收藏")
    @GetMapping("/music/mylist")
    @Parameter(name = "user", description = "用户对象，封装查询条件")
    @Parameter(name = "page", description = "分页信息")
    public PageInfo<Music> selectmyMusic(User user, PageQueryBean page) {
        // 分页设置
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        log.info("查询的user_id:" + user.getUser_id());
        // 查询

        ArrayList<Music> list = musicService.myMusic(user.getUser_id());
        for(int i=0;i<list.size();i++){
            list.get(i).setUser_img((userService.select1(list.get(0).getMusic_user_name())).getUser_tx());
        }
        return new PageInfo<Music>(list);
    }

    @Operation(summary = "我的音乐")
    @GetMapping("/music/mymusic")
    @Parameter(name = "user", description = "用户对象，封装查询条件")
    @Parameter(name = "page", description = "分页信息")
    public PageInfo<Music> selectwodeMusic(User user, PageQueryBean page) {
        // 分页设置
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        log.info("查询的user_id:" + user.getUser_id());
        // 查询

        ArrayList<Music> list = musicService.wodeMusic(user.getUser_id());
        for(int i=0;i<list.size();i++){
            list.get(i).setUser_img((userService.select1(list.get(i).getMusic_user_name())).getUser_tx());
        }
        System.out.println("*********************************************************************888");
        System.out.println(list);
        return new PageInfo<Music>(list);
    }

    @Operation(summary = "取消收藏")
    @DeleteMapping ("/music/deletesc")
    @Parameter(name = "music", description = "音乐对象")
    public boolean deletesc(Music music) {
        return musicService.deletesc(music);
    }

    @Operation(summary = "我的歌单")
    @GetMapping ("/music/gedanlist")
    @Parameter(name = "user", description = "用户对象")
    public List<String> mysong(User user) {
        Set<String> list = songService.select(user.getUser_id());
        ArrayList<String> list1 = new ArrayList<String>(list);
        System.out.println(list1);
        return list1;
    }

    @Operation(summary = "歌单内容")
    @GetMapping("/music/gedan")
    @Parameter(name = "music", description = "音乐对象，封装图书信息")
    protected PageInfo<Music> update(Song song, PageQueryBean page) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        log.info("查询的歌单名:" + song.getForm_Name());
        ArrayList<Music> list = musicService.gedan(song.getForm_Name());
        return new PageInfo<Music>(list);

    }


    @Operation(summary = "加入歌单")
    @PostMapping("/music/myAdd")
    @Parameter(name = "music", description = "音乐对象")
    public boolean addMusic(@RequestBody Music music) {
        Song song = new Song();
        song.setUser_id(music.getMusic_user_id());
        song.setMusic_id(music.getMusic_id()+"");
        song.setForm_Name("古风");
        System.out.println(song);
        return songService.insert(song);
    }


}
